package com.icss.ah.base.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;

@Controller
@Scope("prototype")
public class UploadAction extends BaseAction{
	private File picture;
	private String pictureFileName;
	private File media;
	private String mediaFileName;
	private File attachment;
	private String attachmentFileName;
	private String fileTitle;
	
	private String width;
	private String height;

	private File fileupload;
	
	public File getFileupload() {
		return fileupload;
	}
	public void setFileupload(File fileupload) {
		this.fileupload = fileupload;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
	public void setMedia(File media) {
		this.media = media;
	}
	public void setMediaFileName(String mediaFileName) {
		this.mediaFileName = mediaFileName;
	}
	
	public void setAttachment(File attachment) {
		this.attachment = attachment;
	}
	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}
	
	public void setFileTitle(String fileTitle) {
		this.fileTitle = fileTitle;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	
	/**
	 * 图片上传
	 * @throws IOException
	 * @throws ServletException
	 */
	public void uploadPic1() throws IOException, ServletException {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getResponse();
		String imageUrl = "/uploadImage";
		PrintWriter out = response.getWriter();
		/* =============禁止缓存============== */
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setContentType("text/html;charset=GB2312");
		
		if(fileupload.exists()){
			Long fileSize = fileupload.length();
			if(fileSize>2097152){
				out.print("overSize");
			} else {
				String newFileName = "B2B" + DateUtils.getCurrentDate("yyyyMMddHHmmssSSS")+new Random().nextInt(999999)+".jpg";
				String savePath = imageUrl + "/";
				String saveURL = savePath + newFileName;
				File pathFile = new File(savePath);
				if (!pathFile.exists()) {
					pathFile.mkdirs();
				}
				FileUtil.copyFile(fileupload, new File(saveURL));
				System.out.println("上传成功:" + newFileName);
				out.print(newFileName);
			}
		}
		
	}
	
	/**
	 * 图片上传
	 * @throws IOException
	 * @throws ServletException
	 */
	public void uploadPicHttp() throws IOException, ServletException {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getResponse();
		String imageUrl = ResourceLoader.getInstance("icss").getString("imageUrl");
		PrintWriter out = response.getWriter();
		/* =============禁止缓存============== */
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setContentType("text/html;charset=GB2312");
		
		if(fileupload.exists()){
			Long fileSize = fileupload.length();
			if(fileSize>2097152){
				out.print("overSize");
			} else {
				String newFileName = "B2B" + DateUtils.getCurrentDate("yyyyMMddHHmmssSSS")+new Random().nextInt(999999)+".jpg";
				int status = FileUtil.uploadFileHttp(newFileName, fileupload);
				if (status == HttpStatus.SC_OK) {
					// 上传成功
					System.out.println("上传成功:" + newFileName);
				} else {
					// 上传失败
					System.out.println("上传失败");
				}
				
				newFileName = imageUrl + newFileName;
				out.print(newFileName);
			}
		}
		
	}
	
	private File file;
	private String fileFileName;
	/**
	 * 图片上传
	 * @throws IOException
	 * @throws ServletException
	 */
	
	/**
	 * 图片上传
	 * @throws IOException
	 * @throws ServletException
	 */
	public void uploadPic() throws IOException, ServletException {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getResponse();
		String uploadUrl = ResourceLoader.getInstance("icss").getString("uploadUrl");
		PrintWriter out = response.getWriter();
		/* =============禁止缓存============== */
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setContentType("text/html;charset=GB2312");
		try{
			if(file.exists()){
				Long fileSize = file.length();
				if(fileSize>2097152){
					out.print("overSize");
				} else {
					String newFileName = "";
					
//					String fileName = file.getName();
					String fileExt = fileFileName.substring(fileFileName.lastIndexOf("."), fileFileName.length());
					newFileName = HttpFileUtil.uploadFileHttp(file, uploadUrl, "B2B", fileExt);
					
					if(newFileName==null||newFileName.equals("")){
						// 上传失败
						System.out.println("上传失败");
					}
//					newFileName = imageUrl + newFileName;
					JSONObject obj = new JSONObject();
					obj.put("url", newFileName);
					out.print(obj);
//					out.print(newFileName);
				}
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		
		
	}
	
	/*public void uploadPic() throws IOException, ServletException {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getResponse();
		String imageUrl = ResourceLoader.getInstance("icss").getString("imageUrl");
		PrintWriter out = response.getWriter();
		 =============禁止缓存============== 
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setContentType("text/html;charset=GB2312");
		
		if(file.exists()){
			Long fileSize = file.length();
			if(fileSize>2097152){
				out.print("overSize");
			} else {
				String newFileName = "B2B" + DateUtils.getCurrentDate("yyyyMMddHHmmssSSS")+new Random().nextInt(999999)+".jpg";
				int status = FileUtil.uploadFileHttp(newFileName, file);
				if (status == HttpStatus.SC_OK) {
					// 上传成功
					System.out.println("上传成功:" + newFileName);
				} else {
					// 上传失败
					System.out.println("上传失败");
				}
				
				newFileName = imageUrl + newFileName;
				JSONObject obj = new JSONObject();
				obj.put("url", newFileName);
				out.print(obj);
//				out.print(newFileName);
			}
		}
		
	}*/
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	/**
	 * @return the fileFileName
	 */
	public String getFileFileName() {
		return fileFileName;
	}
	/**
	 * @param fileFileName the fileFileName to set
	 */
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
}
